
import java.util.concurrent.TimeUnit;

public class Shutdown {
    public static void main(String[] args) throws Exception{
        Runner one=new Runner();
        Thread countThreead=new Thread(one,"CountThread");
        countThreead.start();
        TimeUnit.SECONDS.sleep(1);
        countThreead.interrupt();
        Runner two =new Runner();
        countThreead=new Thread(two,"CountThread");
        countThreead.start();
        TimeUnit.SECONDS.sleep(1);
        two.cancel();
    }
    private static class Runner implements Runnable{
        private long i;
        private volatile boolean on =true;
        @Override
        public void run(){
            while(on&&!Thread.currentThread().isInterrupted()){
                i++;
            }
            System.out.println("Count i = "+i);
        }
        public void cancel(){
            on=false;
        }
    }
}
